package com.zkzx.freight.bill.service;

import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.model.OrderLogisticsVO;
import com.zkzx.freight.bill.mapper.OrderLogisticsMapper;
import com.zkzx.freight.bill.mapper.entity.OrderLogistics;
import com.zkzx.freight.bill.mapper.entity.OrderLogisticsExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/20:30
 * @Description:
 */
@Slf4j
@Service
public class OrderLogisticsService {

    @Resource
    private OrderLogisticsMapper orderLogisticsMapper;

    /**
     * 根据订单id查询运单轨迹
     * @param orderId 订单id
     * @return 运单轨迹
     */
    public List<OrderLogisticsVO> findVOByOrderIdId(Integer orderId){
        List<OrderLogistics> orderLogisticss = findByOrderId(orderId);
        if (CollectionUtils.isEmpty(orderLogisticss)){
            return Collections.emptyList();
        }


       return orderLogisticss.stream().map(orderLogistics ->
               OrderLogisticsVO.builder()
                        .id(orderLogistics.getId())
                        .orderId(orderLogistics.getOrderid())
                        .wayPoint(orderLogistics.getWaypoint())
                        .wayTime(orderLogistics.getWaytime())
                        .latitude(orderLogistics.getLatitude())
                        .longitude(orderLogistics.getLongitude())
                .build()).collect(Collectors.toList());
    }

    /**
     * 根据订单id查询运单轨迹
     * @param orderId 订单id
     * @return 运单轨迹
     */
    public List<OrderLogistics> findByOrderId(Integer orderId){
        OrderLogisticsExample orderLogisticsExample = new OrderLogisticsExample();
        orderLogisticsExample.createCriteria().andOrderidEqualTo(orderId);
        return orderLogisticsMapper.selectByExample(orderLogisticsExample);


    }

    /**
     * 插入轨迹信息
     * @param command 入参
     * @param id 订单id
     */
    public void insert(OrderCommand command, Integer id){
        if (CollectionUtils.isEmpty(command.getOrderLogisticss())){
            return;
        }
        List<OrderLogisticsVO> orderLogisticsVOs = command.getOrderLogisticss();
        orderLogisticsVOs.forEach(orderLogisticsVO->{
            Optional.ofNullable(orderLogisticsVO).ifPresent(ol->{
                OrderLogistics orderLogistics = new OrderLogistics();
                orderLogistics.setOrderid(id);
                orderLogistics.setWaypoint(orderLogisticsVO.getWayPoint());
                orderLogistics.setWaytime(orderLogisticsVO.getWayTime());
                orderLogistics.setLongitude(orderLogisticsVO.getLongitude());
                orderLogistics.setLatitude(orderLogisticsVO.getLatitude());
                orderLogisticsMapper.insert(orderLogistics);
            });

        });

    }


    /**
     * 更新轨迹信息
     * @param command 入参
     * @param id 订单id
     */
    public void update(OrderCommand command, Integer id){
        OrderLogisticsExample orderDeliveryExample = new OrderLogisticsExample();
        orderDeliveryExample.createCriteria().andOrderidEqualTo(id);

        if (!CollectionUtils.isEmpty(command.getOrderLogisticss())){
            orderLogisticsMapper.deleteByExample(orderDeliveryExample);
            insert(command, id);
        }

    }
}
